package com.apress.prospring5.ch5;

import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ControlFlowPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class ControlFlowDemo {
    public static void main(String[] args) {
        ControlFlowDemo ex=new ControlFlowDemo();
        ex.run();
    }
    public void run(){
        TestBean target=new TestBean();
        Pointcut pc=new ControlFlowPointcut(ControlFlowDemo.class,"test");
        Advisor advisor=new DefaultPointcutAdvisor(pc,new SimpleAdvice());
        ProxyFactory pf=new ProxyFactory();
        pf.setTarget(target);
        pf.addAdvisor(advisor);

        TestBean proxy=(TestBean) pf.getProxy();
        System.out.println("\tTrying normal invoke");
        proxy.foo();;
        System.out.println("\n\tTrying under ControlFlowDemo.test");
        test(proxy);
    }
    private void test(TestBean bean){
        bean.foo();
    }
}
